package com.lgcns.board.spring;

import java.io.File;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.security.Principal;

import com.lgcns.board.Article;
import com.lgcns.board.AttachFile;
import com.lgcns.board.BoardService;
import com.lgcns.board.Comment;
import com.lgcns.commons.PagingHelper;
import com.lgcns.commons.WebContents;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

@Controller
@RequestMapping("/bbs")
public class BbsController {
	
	@Autowired
	private BoardService boardService;
	
	@RequestMapping(value="/list", method={RequestMethod.GET, RequestMethod.POST})
	public String list(String boardCd, Integer curPage, String searchWord, Model model)
	{
		if (boardCd == null) 	boardCd = "free";
		if (curPage == null) curPage = 1;
		if (searchWord == null) searchWord = "";

		int numPerPage = 10;
		int pagePerBlock = 10;
		
		int totalRecord = boardService.getTotalRecord(boardCd, searchWord);
		
		PagingHelper pagingHelper = new PagingHelper(totalRecord, curPage, numPerPage, pagePerBlock);
		
		boardService.setPagingHelper(pagingHelper);
		
		int start = pagingHelper.getStartRecord();
		int end = pagingHelper.getEndRecord();

		ArrayList<Article> list = boardService.getArticleList(boardCd, searchWord, start, end);
		String boardNm = boardService.getBoardNm(boardCd);
		
		Integer no = boardService.getListNo();
		Integer prevLink = boardService.getPrevLink();
		Integer nextLink = boardService.getNextLink();
		Integer firstPage = boardService.getFirstPage();
		Integer lastPage = boardService.getLastPage();
		
		int[] pageLinks = boardService.getPageLinks();
		
		model.addAttribute("list", list);
		model.addAttribute("boardNm", boardNm);
		model.addAttribute("no", no);
		model.addAttribute("prevLink", prevLink);
		model.addAttribute("nextLink", nextLink);
		model.addAttribute("firstPage", firstPage);
		model.addAttribute("lastPage", lastPage);
		model.addAttribute("pageLinks", pageLinks);
		model.addAttribute("curPage", curPage);
		model.addAttribute("searchWord", searchWord);
		model.addAttribute("boardCd", boardCd);
		
		return "bbs/list";
		
	}
	
	@RequestMapping(value="/view", method=RequestMethod.GET)
	public String view(Integer articleNo,String boardCd,Integer curPage,String searchWord, Model model) {
		if (boardCd == null) 	boardCd = "free";
		if (curPage == null) curPage = 1;
		if (searchWord == null) searchWord = "";

		int numPerPage = 10;
		int pagePerBlock = 10;
		
		boardService.increaseHit(articleNo);
		
 
		Article thisArticle = boardService.getArticle(articleNo);
		ArrayList<AttachFile> attachFileList = boardService.getAttachFileList(articleNo);
		ArrayList<Comment> commentList = boardService.getCommentList(articleNo);
		Article nextArticle = boardService.getNextArticle(articleNo, boardCd, searchWord);
		Article prevArticle = boardService.getPrevArticle(articleNo, boardCd, searchWord);
		

		int totalRecord = boardService.getTotalRecord(boardCd, searchWord);
		PagingHelper pagingHelper = new PagingHelper(totalRecord, curPage, numPerPage, pagePerBlock);

		boardService.setPagingHelper(pagingHelper);
		int start = pagingHelper.getStartRecord();
		int end = pagingHelper.getEndRecord();

		ArrayList<Article> list = boardService.getArticleList(boardCd, searchWord, start, end);
		String boardNm = boardService.getBoardNm(boardCd);
		Integer no = boardService.getListNo();
		Integer prevLink = boardService.getPrevLink();
		Integer nextLink = boardService.getNextLink();
		Integer firstPage = boardService.getFirstPage();
		Integer lastPage = boardService.getLastPage();
		int[] pageLinks = boardService.getPageLinks();
		
		model.addAttribute("thisArticle", thisArticle);
		model.addAttribute("attachFileList", attachFileList);
		model.addAttribute("commentList", commentList);
		model.addAttribute("nextArticle", nextArticle);
		model.addAttribute("prevArticle", prevArticle);

		model.addAttribute("list", list);
		model.addAttribute("boardNm", boardNm);
		model.addAttribute("no", no);
		model.addAttribute("prevLink", prevLink);
		model.addAttribute("nextLink", nextLink);
		model.addAttribute("firstPage", firstPage);
		model.addAttribute("lastPage", lastPage);
		model.addAttribute("pageLinks", pageLinks);

		model.addAttribute("curPage", curPage);
		model.addAttribute("searchWord", searchWord);
		model.addAttribute("boardCd", boardCd);
		model.addAttribute("articleNo", articleNo);
		
		return "bbs/view";
	}
	
	@RequestMapping(value="/attachFileDel", method=RequestMethod.POST)
	public String attachFileDel(Integer attachFileNo, Integer articleNo, String boardCd, Integer curPage, String searchWord) throws Exception
	{
	
		AttachFile attachFile = boardService.getAttachFile(attachFileNo);
		boardService.deleteFile(attachFile.getAttachFileNo());
	
		searchWord = URLEncoder.encode(searchWord,"UTF-8");
		
		StringBuilder sb = new StringBuilder();
		sb.append("bbs/view?articleNo=");
		sb.append(articleNo);
		sb.append("&boardCd=");
		sb.append(boardCd);
		sb.append("&curPage=");
		sb.append(curPage);
		sb.append("&searchWord=");
		sb.append(searchWord);
		
		return "redirect:/" + sb.toString();
	}
	
	@RequestMapping(value="/commentUpdate", method=RequestMethod.POST)
	public String commentUpdate(Integer commentNo, Integer articleNo, String boardCd, Integer curPage, String searchWord, String memo) throws Exception
	{
	    Comment comment = boardService.getComment(commentNo);
	    comment.setMemo(memo);
	    boardService.updateComment(comment);
	    searchWord = URLEncoder.encode(searchWord, "UTF-8");
	    return "redirect:/bbs/view?articleNo=" + articleNo + "&boardCd=" + boardCd + "&curPage=" + curPage + "&searchWord=" + searchWord;
	}
	
	@RequestMapping(value="/commentAdd", method=RequestMethod.POST)
	public String commentAdd(Integer articleNo, String boardCd, Integer curPage, String searchWord, String memo, Principal principal) throws Exception
	{
		
		Comment comment = new Comment();
		comment.setMemo(memo);
		comment.setEmail(principal.getName());
		comment.setArticleNo(articleNo);
		
		boardService.insertComment(comment);
		
		searchWord = URLEncoder.encode(searchWord,"UTF-8");
	
		StringBuilder sb = new StringBuilder();
		sb.append("bbs/view?articleNo=");
		sb.append(articleNo);
		sb.append("&boardCd=");
		sb.append(boardCd);
		sb.append("&curPage=");
		sb.append(curPage);
		sb.append("&searchWord=");
		sb.append(searchWord);
	
		return "redirect:/" + sb.toString();
	}
	
	@RequestMapping(value="/commentDel", method=RequestMethod.POST)
	public String commentDel(Integer commentNo, Integer articleNo, String boardCd, Integer curPage, String searchWord) throws Exception
	{
		
		Comment comment = boardService.getComment(commentNo);
		boardService.deleteComment(comment);
	
		searchWord = URLEncoder.encode(searchWord,"UTF-8");
	
		StringBuilder sb = new StringBuilder();
		sb.append("bbs/view?articleNo=");
		sb.append(articleNo);
		sb.append("&boardCd=");
		sb.append(boardCd);
		sb.append("&curPage=");
		sb.append(curPage);
		sb.append("&searchWord=");
		sb.append(searchWord);
		
		return "redirect:/" + sb.toString();
}
	
	@RequestMapping(value="/write", method=RequestMethod.GET)
	public String write(Integer articleNo, String boardCd, Integer curPage, String searchWord, Model model)
	{

	    String boardNm = boardService.getBoardNm(boardCd);

	    model.addAttribute("curPage", curPage);
	    model.addAttribute("searchWord", searchWord);
	    model.addAttribute("boardCd", boardCd);
	    model.addAttribute("articleNo", articleNo);
	    model.addAttribute("boardNm", boardNm);
	     
	    return "bbs/writeform";
	}
	
	@RequestMapping(value="/write", method=RequestMethod.POST)
	public String write(Article article, Principal principal) {
		
		article.setEmail(principal.getName());
				
		boardService.insert(article);
		
		return "redirect:/bbs/list?boardCd=" + article.getBoardCd();	
	}
	
	@RequestMapping(value="/modify", method=RequestMethod.GET)
	public String modify(Integer articleNo, String boardCd, Integer curPage, String searchWord, Model model)
	{
	     
	    Article thisArticle = boardService.getArticle(articleNo);
	    String boardNm = boardService.getBoardNm(boardCd);
	     

	    model.addAttribute("thisArticle", thisArticle);
	    model.addAttribute("curPage", curPage);
	    model.addAttribute("searchWord", searchWord);
	    model.addAttribute("boardCd", boardCd);
	    model.addAttribute("articleNo", articleNo);
	    model.addAttribute("boardNm", boardNm);
	     
	    return "bbs/modifyform";
	}
	
	@RequestMapping(value="/modify", method=RequestMethod.POST)
	public String modify(MultipartHttpServletRequest mpRequest) throws Exception
	{
		int articleNo = Integer.parseInt(mpRequest.getParameter("articleNo"));
		
		Article article = boardService.getArticle(articleNo);
		
		String boardCd = mpRequest.getParameter("boardCd");
		int curPage = Integer.parseInt(mpRequest.getParameter("curPage"));
		
		String searchWord = mpRequest.getParameter("searchWord");
		
		String title = mpRequest.getParameter("title");
		String content = mpRequest.getParameter("content");
		
		article.setTitle(title);
		article.setContent(content);
		article.setBoardCd(boardCd);
		boardService.update(article);

		Iterator<String> it = mpRequest.getFileNames();
		List<MultipartFile> fileList = new ArrayList<MultipartFile>();
		
		while (it.hasNext())
		{
			MultipartFile multiFile = mpRequest.getFile((String) it.next());
			if (multiFile.getSize() > 0)
			{
				String filename = multiFile.getOriginalFilename();
				multiFile.transferTo(new File(WebContents.BASE_PATH + filename));
				fileList.add(multiFile);
			}
		}
		
		int size = fileList.size();
		
		for (int i = 0; i < size; i++)
		{
			MultipartFile mpFile = fileList.get(i);
			AttachFile attachFile = new AttachFile();
			String filename = mpFile.getOriginalFilename();
			
			attachFile.setFilename(filename);
			attachFile.setFiletype(mpFile.getContentType());
			attachFile.setFilesize(mpFile.getSize());
			attachFile.setArticleNo(articleNo);
			
			boardService.insertAttachFile(attachFile);
		}
		
		searchWord = URLEncoder.encode(searchWord,"UTF-8");
		
		StringBuilder sb = new StringBuilder();
		sb.append("bbs/view?articleNo=");
		sb.append(articleNo);
		sb.append("&boardCd=");
		sb.append(boardCd);
		sb.append("&curPage=");
		sb.append(curPage);
		sb.append("&searchWord=");
		sb.append(searchWord);

		return "redirect:/" + sb.toString();
		
	}
	
	
	@RequestMapping(value="/delete", method=RequestMethod.POST)
	public String delete(Integer articleNo, String boardCd, Integer curPage, String searchWord) throws Exception
	{
	
	Article article = boardService.getArticle(articleNo);
	boardService.delete(article);
	
	searchWord = URLEncoder.encode(searchWord, "UTF-8");
	
	StringBuilder sb = new StringBuilder();
	sb.append("bbs/list?boardCd=");
	sb.append(boardCd);
	sb.append("&curPage=");
	sb.append(curPage);
	sb.append("&searchWord=");
	sb.append(searchWord);
	
	return "redirect:/" + sb.toString();
	
	}
	
	@RequestMapping(value="/download", method=RequestMethod.POST)
	public String download(String filename, Model model)
	{
		model.addAttribute("filename", filename);
		return "inc/download";
	}
}